@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/admin/new-precut-transfer/save" method="post" class="main-form">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <div class="form-item form-s1">
                <div class="form-label" required="">申请套件ID</div>
                <div class="form-input">
                    <select name="precut_id" id="" class="form-control sq-rolls"></select>
                </div>
            </div>


            @if(session('admin.user_type')==2)
            @else
                <div class="form-item form-s1">
                    <div class="form-label">{{ ts('DHFQsd') }}</div>
                    <div class="form-input">
                        <input name="ysdmc" id="" class="form-control" readonly>
                    </div>
                </div>
            @endif
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('DHFQJXS') }}</div>
                <div class="form-input">
                    <select name="ygmsid" id="" class="form-control"></select>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label">{{ ts('DHsd') }}</div>
                <div class="form-input">
                    <select name="xsdid" id="xsdid" class="form-control">
                        @foreach(\App\Models\Admin::where(['status' => 1, 'user_type'=>2])->orderBy('unique_id')->get(['id', 'abbr', 'company_name']) as $v)
                            <option value="{{ $v->id }}" {{ session('admin.id') == $v->id ? 'selected' : '' }}>{{ $v->abbr ?: $v->company_name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('DHjxs') }}</div>
                <div class="form-input">
                    <select name="xgmsid" id="" class="form-control sq-jxs"></select>
                </div>
            </div>


            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('EXPLAIN') }}</div>
            </div>
            <div class="form-item form-s1 px-4">
                <textarea name="note" id="" cols="30" rows="10" class="form-control" required></textarea>
            </div>
            <div class="mt-4">
                <button type="submit" class="btn btn-info">{{ ts('SUBMIT') }}</button>
            </div>
        </form>

    </div>


@endsection

@section('script')
    <script>
        var ADMINID = '{{ session('admin.id') }}';
        $(document).ready(function () {
            $('body').on('submit', '.main-form', function () {
                $(this).find('button[type="submit"]').attr('disabled', true);
                var that = $(this);
                var form = formObjV2('.main-form');
                var state = true;
                if (state && !form.precut_id) {
                    toastr.error('申请套件Id不能为空！');
                    state = false;
                }
                if (state && !form.ygmsid) {
                    return confirm('发起方-购买不能为空！');
                    state = false;
                }
                if (state && !form.xsdid && !form.xgmsid) {
                    toastr.error('接收方 省代与购买商 不能同时为空！');
                    state = false;
                }
                if (typeof ADMINID != 'undefined') {
                    if (state && form.xsdid == ADMINID && form.ygmsid == ADMINID) {
                        // alert('自己给下属门店调货请使用分配功能！');
                        toastr.error('自己给下属门店调货请使用分配功能！');
                        state = false;
                    }
                }
                if (state && !form.note) {
                    toastr.error('说明不能为空！');
                    state = false;
                }
                if (state && !form.xgmsid) {
                    return confirm('接收方-购买商为空时，将调货给接收方-省代，确认调货？');
                }
                if (state) {
                    return true;
                }  else {
                    setTimeout(function () {
                        that.find('button[type="submit"]').attr('disabled', false);
                    }, 500);
                    return false;
                }
            })
        });

        function getPrecutData(precut_id, user_id) {
            precut_id = String(precut_id).trim();
            if (precut_id === '' || precut_id==='null' || precut_id===null) {
                $('select[name="ygmsid"]').empty();
                return false
            }
            request.get('/admin/new-precut-transfer/getPrecutDetail', {
                params: {
                    precut_id: precut_id,
                    user_id : user_id
                }
            }).then( function(d) {
                var data = d.data;
                console.log(data);
                $('select[name="ygmsid"]').val(' ');
                $('select[name="ygmsid"]').empty().append(makeSelectOptionsOnly(data));
                $('[name="ygmsid"]').trigger('change');
                if (data.length < 1) {
                    toastr.error('套件数据有误！');
                    $('body .sq-rolls').select2('val', ' ');
                } else {
                    //getResidueLengthByRollNumberAndGmsId()
                }
            })
        }

        $(document).ready(function () {
            if ($('[name="xsdid"]').length >= 1) {
                $('[name="xsdid"]').on('change', function () {
                    $('.sq-jxs').select2('val', ' ');
                })
                $('[name="xgmsid"]').val(' ')
            }

            $('select[name="precut_id"]').on('change', function () {
                var val = $(this).val();
                var data = rolls_front_data[val];
                console.log(data);
                getPrecutData(val, data.user_id);
            })
            $('select[name="ygmsid"]').on('change', function () {
                //getResidueLengthByRollNumberAndGmsId()
            })
            $('.sq-jxs').select2({
                ajax: {
                    type:'get',
                    url: '/admin/ajax/getJXSMC',
                    dataType: 'json',
                    delay: 250,dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            sdid: $('select[name="xsdid"]').val(),
                            status: 1
                        };
                    },
                    processResults: function (data) {
                        data = data['data']
                        items = []
                        for (var i = 0; i <= data.length - 1; i++) {
                            items[i] = {
                                id: data[i].id,
                                text: data[i].abbr
                            }
                        }
                        return {
                            results: items
                        };
                    },
                },
                selectOnClose: true
            });

            $('select.sq-rolls').select2({
                ajax: {
                    type:'get',
                    url: '/admin/new-precut-transfer/getPrecut',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                        };
                    },
                    processResults: function (data) {
                        data = data['data']
                        items = []
                        for (var i = 0; i <= data.length - 1; i++) {
                            items[i] = {
                                id: data[i].precut_id,
                                text: data[i].precut_id
                            }
                            window.rolls_front_data[data[i].precut_id] = data[i]
                        }
                        return {
                            results: items
                        };
                    },
                },
                selectOnClose: true
            });
            $('[name="ygmsid"]').on('change', function () {
                console.log('hello world');
                console.log($(this).val());
                getSdmc($(this).val()).then(function (d) {
                    $('[name="ysdmc"]').val(d.data)
                })
            });
            $('[name="xgmsid"]').on('change', function () {
                getSdmc($(this).val()).then(function (d) {
                    $('[name="xsdmc"]').val(d.data)
                })
            });
            // $('[name="xgmsid"]').trigger('change')
        });

        function getSdmc(id) {
            return new Promise(function (resolve, reject) {
                if (!id) {
                    return reject(null)
                }
                request.get('/admin/ajax/getSdmc', {
                    params: {
                        user_id: id
                    }
                })
                    .then(function (d) {
                        resolve(d)
                    })
                    .catch(function (d) {
                        reject(d)
                    })
            })
        }


        function getXsdmc() {

        }
        function getYsdmc() {

        }

    </script>

@endsection